<?php

class TaskController extends Zend_Controller_Action {

    public function indexAction() {
        
    }
    
    public function saveAction() {
        header("Content-Type: application/json");

        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        
        $request = $this->getRequest();
                
        if ($request->isGet()) {
            
            $name = $request->getParam("name");
            $plannedTime = $request->getParam("planned_time");
            $isActivity = $request->getParam("id_activity");
            
            $taskModel = new Application_Model_TaskModel();
            $taskToSave = array(
                "nombre" => $name,
                "planned_time" => $plannedTime,
                "idactivities" => $isActivity
            );
            $message = "Error al guardar Tarea";
            $error = true;
            if ($taskModel->insert($taskToSave)) {
                $message = "Tarea guardada!";
                $error = false;
            }
            
            $arrayResponse = array(
                                    "message" => $message,
                                    "error" => $error
                            );
            $jsonResponse = json_encode($arrayResponse);

            echo $jsonResponse;
        }
    }

    public function getProgressTaskAction(){
        header("Content-Type: application/json");

        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();

        $request = $this->getRequest();
        $idTask = $request->getParam("idTask");

        $tableTask = new Application_Model_TaskModel();
        $select = $tableTask->fetchRow("idtasks = ".$idTask);
        $task = $select->toArray();

        $timeProgressCurrent = $task['progress_time'];
        $plannedTime = $task['planned_time'];

        $time = $timeProgressCurrent + 1;
        echo $time;
        $data = array(
            "progress_time" => $time
        );
        
        $where = $tableTask->getAdapter()->quoteInto("idtasks=?", $idTask);

        $tableTask->update($data, $where);

        $percent = ($time*100)/$plannedTime;

        $arrayResponse = array(
            "progres_percent" => $percent
        );
        $jsonResponse = json_encode($arrayResponse);
        
        echo $jsonResponse;
        
    }
    public function setStatusPauseAction(){
        header("Content-Type: application/json");

        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        
        $STATUS_PAUSE = 3;
        $this->changeStatus($STATUS_PAUSE);
    }

    public function setStatusEnProgresoAction(){
        header("Content-Type: application/json");

        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();

        $STATUS_PROGRESO = 2;
        $this->changeStatus($STATUS_PROGRESO);
    }

   public function setStatusIniciarAction(){
        $STATUS_NUEVO = 1;
        $this->changeStatus($STATUS_PAUSE);
    }
    public function setStatusCancelarAction(){
        $request = $this->getRequest();
        $idTask = $request->getParam("id");
        $tableTask = new Application_Model_TaskModel();
        $where = $tableTask->getAdapter()->quoteInto("idtasks=?", $idTask);
        $tableTask->delete($where);
    }

    private function changeStatus($status){
        $request = $this->getRequest();
        $idTask = $request->getParam("id");
        $data = array(
            "status" => $status
        );

        $tableTask = new Application_Model_TaskModel();
        $where = $tableTask->getAdapter()->quoteInto("idtasks=?", $idTask);

        $tableTask->update($data, $where);
    }
}